package lee.study.fanshe;

public enum TestEnum {
    RED(1, "红色"), BLACK(2, "黑色"), GREEN(3, "绿色"), WHITE(4, "白色");//枚举对象
    int ordinal;
    String color;

    TestEnum(int ordinal, String color) {//枚举的构造方法是私有的！！！
        this.ordinal = ordinal;
        this.color = color;
    }

    public static TestEnum getEnumOrdinal(int ordinal) {
        for (TestEnum T : TestEnum.values()) {
            if (T.ordinal == ordinal) {
                return T;
            }
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(getEnumOrdinal(3));
    }

    public static void main3(String[] args) {//枚举的比较   compareTo
        TestEnum testEnum1 = TestEnum.RED;
        TestEnum testEnum2 = TestEnum.BLACK;
        System.out.println(testEnum1.compareTo(testEnum2));

        System.out.println(RED.compareTo(BLACK));
        System.out.println(BLACK.compareTo(RED));
    }

    public static void main2(String[] args) {//将普通字符串转换为枚举  valueOf
        TestEnum[] testEnums1 = TestEnum.values();
        for (int i = 0; i < testEnums1.length; i++) {
            System.out.println(testEnums1[i] + " " + testEnums1[i].ordinal());
        }
        System.out.println("===========");
        System.out.println(TestEnum.valueOf("BLACK"));
    }

    public static void main1(String[] args) {
        TestEnum testEnum = TestEnum.BLACK;
        switch (testEnum) {
            case RED:
                System.out.println("RED");
                break;
            case BLACK:
                System.out.println("BLACK");
                break;
            case GREEN:
                System.out.println("GREEN");
                break;
            case WHITE:
                System.out.println("WHITE");
                break;
            default:
                break;
        }
    }
}
